import PageLoading from '@/components/Global/PageLoading'
import { api } from '@/servers/api'
import { postData } from '@/servers/http'
import { getLocalStorage } from '@/storage'
import { setUserCenterCookies } from '@/utils/userCenter'
import { Toast } from 'antd-mobile'
import React, { useEffect } from 'react'
import { history } from 'umi'
import wx from 'weixin-webview-jssdk'

const ArchiveMiddle = () => {
    const { id, questionnaireCode, resultCode, messageCode, success, message }: any = history.location.query || {}
    const userData = getLocalStorage('USER_DATA')
    const identityType = getLocalStorage('IDENTITY_TYPE')

    /** 跳转详情页 */
    const jumpDetail = () => {
        history.replace({ pathname: '/archivedetail', query: { id } })
    }
    /** 跳转平台问卷页面 */
    const jumpPlatform = () => {
        const { platform_access_token, mid_domain, platform_site_id = '', imitate_site_id = '', platform_site_alias, imitate_site_alias, platform_usercode } = userData || {}
        let callbackUrl = ''
        callbackUrl = encodeURIComponent(window.location.origin + `/#/archivemiddle?id=${id}&questionnaireCode=${questionnaireCode}`)
        setUserCenterCookies(
            {
                h5Token: platform_access_token,
                h5SelectUserType: '1',
                h5UserCode: platform_usercode,
                h5Alias: Number(identityType) === 2 ? imitate_site_alias : platform_site_alias,
            },
            Number(identityType) === 2 ? imitate_site_id : platform_site_id,
        )
        const jumpUrl = `${mid_domain}/question-center/collecting?code=${questionnaireCode}&h5Back=${callbackUrl}&fill=edit` + (resultCode ? `&collectCode=${resultCode}` : '')
        window.location.replace(jumpUrl)
    }
    /** 校验同步 */
    const verifySync = () => {
        if (!resultCode) {
            jumpDetail()
        }
        postData(`${api.userBarriersBatchInput}${id}`, { fill_type: 1, question_code: questionnaireCode, result_code: resultCode })
            .then(({ status, data }) => {
                if (status === 200 && data) {
                    if (Number(identityType) !== 2 && data?.gain_medal) {
                        wx.miniProgram.navigateTo({
                            url: `/pageStudent/pages/gotMedal/index?medalData=${encodeURIComponent(JSON.stringify(data.gain_medal))}`,
                        })
                    }
                    jumpDetail()
                }
            })
            .catch(() => {
                Toast.show('提交失败')
                jumpPlatform()
            })
    }
    /** 返回失败提示 */
    const failToast = () => {
        switch (messageCode) {
            case 'BUSINESS_QUESTIONNAIRE_NO_PUBLISH_ERROR':
                Toast.show('问卷已停止收集')
                jumpDetail()
                break
            case 'BUSINESS_QUESTIONNAIRE_TIME_UPDATE':
                Toast.show('问卷内容已更新')
                jumpPlatform()
                break
            default:
                if (success === 'false') {
                    Toast.show(message || '提交失败')
                    jumpPlatform()
                }
                break
        }
        verifySync()
    }
    useEffect(() => {
        failToast()
    }, [])

    return <PageLoading />
}

export default ArchiveMiddle
